--Target Bar
local BarStatus = MClass("BarStatus")

local specialHPPanelWidth = 183 -- 特殊血条 panel宽

--ctor 初始化UI init设置静态数据 setData设置动态数据
function BarStatus:ctor()
    --self.root 默认为根节点
    self.hpBarFoe = self:findChild("hp_foe")
    self.hpBarPal = self:findChild("hp_team")
	self.hpBarFoeExtra = self:findChild("hp_foe_extra")
    self.mpBarFoe = self:findChild("Panel_mp/mp_2")
    self.mpBarPal = self:findChild("Panel_mp/mp_1")
    self.num = self:findChild("num")
	self.numExtra = self:findChild("num_extra")
	self.specialHPPanelWidth = specialHPPanelWidth
    self.specialHPSign = self:findChild("hp_foe/Panel")
    if self.specialHPSign then
    	self.specialHPPanelWidth = self.specialHPSign:getContentSize2()
    end

	self:init()
end

function BarStatus:setSpecialHPVisible(bSpecial)
	if self.specialHPSign then
    	self.specialHPSign:setVisible(bSpecial)
    end
end

function BarStatus:init()
	self.hpBar = self.hpBarFoe
	self.hpBarFoe:setVisible(false)
	self.hpBarPal:setVisible(false)
	if self.mpBarFoe then
		self.mpBarFoe:setVisible(false)
		self.mpBar = self.mpBarFoe
	end
	if self.mpBarPal then
		self.mpBarPal:setVisible(false)
	end
	if self.hpBarFoeExtra then
		self.hpBarFoeExtra:setVisible(false)
	end
	if self.numExtra then
		self.numExtra:setVisible(false)
	end
end

function BarStatus:setData(view, entityModel)
	--设置Bar种类
	self:setEnemy(view:getActorType(), entityModel:hasRelatedNpc())

	local maxHP = view:getSpecialHPMax()
	local isSpecialHP = maxHP > 0
	if self.specialHPSign and isSpecialHP then
		self:setSpecialHPVisible(true)
		local shownNum = 0
		local nodeListTemp = {}
		for i = 1, Constant.BattleSpecialHPNodeMaxNum do
			local ndoeTemp = self.specialHPSign:getChildByName( "Image_" .. i )
			table.insert( nodeListTemp, ndoeTemp )

			if i < maxHP then
				shownNum = shownNum + 1
				ndoeTemp:setVisible( true )
			else
				ndoeTemp:setVisible( false )
			end
		end
		local distanceX = self.specialHPPanelWidth/( shownNum + 1 )
		for i,v in ipairs( nodeListTemp ) do
			local posX = 0 + distanceX*i
			v:setPositionX( posX )
		end
	else
		self:setSpecialHPVisible(false)
	end

	if self.mpBar then
		self.mpBar:setVisible(view:getIsShowMana())
	end

	if entityModel:hasRelatedNpc() then
		if self.hpBarFoeExtra then
			self.hpBarFoeExtra:setVisible(true)
		end
		if self.numExtra then
			self.numExtra:setVisible(true)
		end
	end
end

function BarStatus:setEnemy(actorType, hasRelatedNpc)
	if actorType == Enums.BattleActorType.self or actorType == Enums.BattleActorType.teammate then
		self.hpBarFoe:setVisible(false)
		if self.mpBarFoe then
   			self.mpBarFoe:setVisible(false)
		end
   		self.hpBarPal:setVisible(true)
		if self.mpBarPal then
   			self.mpBarPal:setVisible(true)
		end
   		self.hpBar = self.hpBarPal
		self.mpBar = self.mpBarPal
	else
		self.hpBarFoe:setVisible(true)
		self.hpBarPal:setVisible(false)
		if self.mpBarFoe then
			self.mpBarFoe:setVisible(true)
		end
		if self.mpBarPal then
			self.mpBarPal:setVisible(false)
		end
		if hasRelatedNpc then
			if self.hpBarFoeExtra then
				self.hpBarFoeExtra:setVisible(true)
			end
			if self.numExtra then
				self.numExtra:setVisible(true)
			end
		else
			if self.hpBarFoeExtra then
				self.hpBarFoeExtra:setVisible(false)
			end
			if self.numExtra then
				self.numExtra:setVisible(false)
			end
		end

   		self.hpBar = self.hpBarFoe
		self.mpBar = self.mpBarFoe
	end
end

function BarStatus:updateData(hpMin, hpMax, manaMin, manaMax, rageMin, rageMax, energyMin, energyMax, hpExtra, hpMaxExtra)
	local precent = math.floor(100*hpMin/hpMax)
	self.hpBar:setPercent(precent)
	local ball = self.hpBar:getSlidBallRenderer()
	if precent>=93 or precent<=7 then
		ball:setVisible(false)
	else
		ball:setVisible(true)
	end

	if self.mpBar then
		if manaMax > 0 then
			precent = math.floor(100*manaMin/manaMax)
		elseif rageMax > 0 then
			precent = math.floor(100*rageMin/rageMax)
		elseif energyMax > 0 then
			precent = math.floor(100*energyMin/energyMax)
		end
		self.mpBar:setPercent(precent)
		ball = self.mpBar:getSlidBallRenderer()
		if precent>=93 or precent<=7 then
			ball:setVisible(false)
		else
			ball:setVisible(true)
		end
	end
	self.num:setString(hpMin.."/"..hpMax)

	if hpMaxExtra > 0 then
		local precent = math.floor(100*hpExtra/hpMaxExtra)
		self.hpBarFoeExtra:setPercent(precent)
		local ball = self.hpBarFoeExtra:getSlidBallRenderer()
		if precent>=93 or precent<=7 then
			ball:setVisible(false)
		else
			ball:setVisible(true)
		end
		self.numExtra:setString(hpExtra.."/"..hpMaxExtra)
	end
end

-- 隐藏血条显示
function BarStatus:shouldHideAllHpUI(flag)
	-- 隐藏血条时特效的显示
	if cc.isDead(self.hpHideEffectNode) then
		local params =
		{
			path = Res.HideHpEffect_1.dir,
			armatureName = Res.HideHpEffect_1.name,
			animationName = "",
			skeletonName = Res.HideHpEffect_1.name
		}
		local Armature = app:getClass("Armature")
		self.hpHideEffectNode = Armature.create( params )
		self.hpBar:getParent():addChild( self.hpHideEffectNode )
		self.hpHideEffectNode:setPosition( self.hpBar:getPosition() )
	end
	self.hpHideEffectNode:setVisible(flag)
	if flag then
		if not self.hpHideEffectNodeAniPlayed then
			self.hpHideEffectNode:play("effcreat", 1, function()
				self.hpHideEffectNode:play("effloop", 1, function()
					self.hpHideEffectNode:play("effdead", 1)
				end )
			end )
			self.hpHideEffectNodeAniPlayed = true
		end
	else
		self.hpHideEffectNodeAniPlayed = false
	end
end

function BarStatus:clearHideEffect()
	if not cc.isDead(self.hpHideEffectNode) then
		self.hpHideEffectNode:removeFromParent()
		self.hpHideEffectNode = nil
	end
end

return BarStatus
